package com.shire.shire_interface.controller.invite;

import com.alibaba.fastjson.JSONObject;
import com.shire.shire_interface.common.bin.Response;
import com.shire.shire_interface.controller.BaseController;
import com.shire.shire_interface.model.invite.WxInviteSubTask;
import com.shire.shire_interface.model.invite.WxInviteSubTaskExample;
import com.shire.shire_interface.model.invite.WxInviteTask;
import com.shire.shire_interface.model.invite.WxInviteTaskExample;
import com.shire.shire_interface.service.friend.WxFriendService;
import com.shire.shire_interface.service.invite.WxInviteSubTaskService;
import com.shire.shire_interface.service.invite.WxInviteTaskService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

//@Api(tags = "加好友 添加好友/添加群好友")
@RestController
@RequestMapping("/invite")
public class WxInviteSubTaskController extends BaseController {

    @Resource
    private WxInviteSubTaskService wxInviteSubTaskService;
    @Resource
    private WxInviteTaskService wxInviteTaskService;
    @Resource
    private WxFriendService wxFriendService;


//    @ApiOperation(value = "邀请记录列表 csId, wxAccount, type: 2：添加群成员,  其它不用填写")
//    @ApiImplicitParams(value = {
//            @ApiImplicitParam(paramType = "body", required = true, dataType = "WxInviteTask")})
//    @ApiResponses({@ApiResponse(code = 200, message = "调用成功返回", response = Response.class)})
    @PostMapping(value = "/inviteFriendHistory")
    @ResponseBody
    public Response getInviteCsHistory(@RequestBody JSONObject jsonObject, HttpSession session) {
        Response response = new Response();
        try {
            Integer csId = jsonObject.getInteger("csId");

            if (!checkLoginUserByCsId(session, csId)){
                response.error("请登录！");
                response.setCode(501);
                return response;
            }

            Integer type = jsonObject.getInteger("type");
            String wxAccount = jsonObject.getString("wxAccount");


            WxInviteTaskExample example = new WxInviteTaskExample();
            WxInviteTaskExample.Criteria criteria;
            if (type!=null && type.equals(2)){
                criteria = example.createCriteria().andCsidEqualTo(csId).andTypeEqualTo(2).andTaskstatusEqualTo(0);
            }else {
                criteria = example.createCriteria().andCsidEqualTo(csId).andTypeNotEqualTo(2).andTaskstatusEqualTo(0);
            }

            addPageParamToExample(jsonObject,example);

            if (jsonObject.containsKey("wxAccount")){
                criteria.andWxaccountidEqualTo(wxAccount);
            }

            example.setOrderByClause("createTime desc");

            List<WxInviteTask> csHistorys = wxInviteTaskService.getInviteCsHistory(example);

            WxInviteSubTaskExample wxInviteSubTaskExample = new WxInviteSubTaskExample();

            for (WxInviteTask csHistory : csHistorys) {
                wxInviteSubTaskExample.clear();
                Integer id = csHistory.getId();
                wxInviteSubTaskExample.createCriteria().andCsidEqualTo(csHistory.getCsid()).andWxaccountEqualTo(csHistory.getWxaccountid()).andTaskidEqualTo(id);
                List<WxInviteSubTask> inviteHistorys = wxInviteSubTaskService.getInviteHistory(wxInviteSubTaskExample);
                csHistory.setWxInviteSubTaskList(inviteHistorys);
            }
            response.setData(csHistorys);
            response.success("成功");
        } catch (Exception e) {
            response.error("失败");
            e.printStackTrace();
        }
        return response;
    }
}
